<?php
/**********************
**dba.inc
**Date Created: 4/13/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/

require_once("shared/errors.inc");

define("HOST", "localhost");
define("USER", "root");
define("PASS", "intellimation");
define("DB", "Intellimation");

class DBaccess
{
	private $host;
	private $user;
	private $pass;
	private $db;
	private $connection;
	private $error;
	
	public function __construct()
	{
		$this->host = HOST;
		$this->user = USER;
		$this->pass = PASS;
		$this->db = DB;	
		$this->error = null;
		$this->connection = null;
	}
	
	public function __destruct()
	{
		if( $this->connection != null )
		{
			$this->closeConnection();
		}
	}
	
	public function executeQuery($query, &$result)
	{
		$rc = $this->openConnection();
		if( TT_FAILED($rc) )
		{
			return $rc;
		}
		$result = mysql_query($query);
		if( !$result )	
		{
			$this->setLastError(mysql_error());
			$this->closeConnection();
			return TT_GENERAL;
		}
		$this->closeConnection();
		return TT_OK;
	}
	public function getLastError()
	{
		return $this->error;
	}
	
	private function openConnection()
	{
		//INFO:  If a second call is made to mysql_connect()  with the same arguments, no new link will be 
		//		established, but instead, the link identifier of the already opened link will be returned.
		$rc = TT_OK;
		$this->connection = mysql_connect($this->host, $this->user, $this->pass);
		if( !$this->connection )
		{
			$this->setLastError("Unable to open connection to MySQL server: " . mysql_error());
			return TT_FAILED;
		}
		if( !mysql_select_db($this->db) )
		{
			$this->setLastError("Unable to select database");
			$this->closeConnection();
			return TT_FAILED;
		}
		return TT_OK;
	}
	
	private function closeConnection()
	{
		mysql_close($this->connection);
		$this->connection = null;
	}
	
	private function setLastError($error)
	{
		$this->error = $error;
	}

	static function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
	{
		$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

		if( function_exists("mysql_real_escape_string") )
		{
			$connection = new DBaccess();
			$rc = $connection->openConnection();
			if( TT_FAILED($rc) )
			{
				//Later attempts should fail anyway
				return 'NULL';
			}
			//WARNING: this function requires an already active DB connection!
			$theValue = mysql_real_escape_string($theValue, $connection->connection);
		}
		else
		{
			$theValue = mysql_escape_string($theValue);
		}

		switch ($theType) {
		case "text":
			$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
			break;    
		case "long":
		case "int":
			$theValue = ($theValue != "") ? intval($theValue) : "NULL";
			break;
		case "double":
			$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
			break;
		case "date":
			$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
			break;
		case "defined":
			$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
			break;
		}
		return $theValue;
	}
}

?>
